package net.example.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import net.example.manager.ClientGlobalCache;
import net.example.model.event.RequestAddFriendEvent;
import net.example.model.event.RequestAddGroupEvent;
import net.example.ggtalk.R;
import net.example.utils.IntentUtils;
import net.example.utils.ToastUtils;

import de.greenrobot.event.EventBus;

public class ValidationMessageActivity extends Activity {
    private TextView tv_newfriendsCount;
    private TextView tv_newfriendsCount4JoinGroup;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_validation_message);
        ((View)findViewById(R.id.ct_photo)).setVisibility(View.GONE);
        TextView pageTitle=(TextView)findViewById(R.id.headerText);
        pageTitle.setText("验证消息");
        tv_newfriendsCount=(TextView) this.findViewById(R.id.tv_newfriendsCount);
        tv_newfriendsCount4JoinGroup=(TextView) this.findViewById(R.id.tv_item_value);

        View newFriend=this.findViewById(R.id.newFriend);
        newFriend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentUtils.startActivity(ValidationMessageActivity.this, FriendNotifyActivity.class);
            }
        });
        View newGroup=this.findViewById(R.id.newGroup);
        newGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentUtils.startActivity(ValidationMessageActivity.this, GroupNotifyActivity.class);
            }
        });

        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    public void onEventMainThread(RequestAddFriendEvent event)
    {
        this.setNewfriendsCount();
    }
    /*设置新被邀请加好友的数量*/
    private void setNewfriendsCount()
    {
        try {
            int newFriendsCount= ClientGlobalCache.getInstance().getRequestAddFriendCount();
            if(newFriendsCount==0)
            {
                tv_newfriendsCount.setVisibility(View.GONE);
            }else {
                tv_newfriendsCount.setText(String.valueOf(newFriendsCount));
                tv_newfriendsCount.setVisibility(View.VISIBLE);
            }
        }catch (Exception ex)
        {
            ToastUtils.showLongTemp(this,ex.getMessage());
        }
    }

    public void onEventMainThread(RequestAddGroupEvent event)
    {
        this.setNewfriendsCount4JoinGroup();
    }

    /*设置新申请加入组的成员数量*/
    private void setNewfriendsCount4JoinGroup()
    {
        try {
            int newFriendsCount=ClientGlobalCache.getInstance().getRequestAddGroupCount();
            if(newFriendsCount==0)
            {
                tv_newfriendsCount4JoinGroup.setVisibility(View.GONE);
            }else {
                tv_newfriendsCount4JoinGroup.setText(String.valueOf(newFriendsCount));
                tv_newfriendsCount4JoinGroup.setVisibility(View.VISIBLE);
            }
        }catch (Exception ex)
        {
            ToastUtils.showLongTemp(this,ex.getMessage());
        }
    }
    public void finish(View view)
    {
        finish();
    }

    @Override
    public void onStart(){
        super.onStart();
        this.setNewfriendsCount();
        this.setNewfriendsCount4JoinGroup();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}